const {
    saveRolePermissionServe,
    getRolePermissionByIdServe,
    updateRolePermissionServe
} = require('../serve/rolePermission.serve')
const {getMenuServe} = require('../serve/menu.serve')
const {dataToTree} = require('../utils/dataConversion')
const {getRoleById} = require('../serve/role.serve')

class RolePermissionController {
    async saveRolePermission(ctx) {
        const {roleId, permissionId} = ctx.request.body
        try {
            const currentRole = await getRoleById(roleId)
            if (currentRole.length === 0) {
                return ctx.body = {
                    status: 1,
                    msg: '未找到当前角色'
                }
            }
            let updateRolePermissionRes;
            const currentMenu = await getMenuServe(permissionId)
            const menus = await currentRole.getMenus();
            if (menus.length > 0) {
                console.log('更新权限');
                updateRolePermissionRes = await currentRole.setMenus(currentMenu);
            } else {
                console.log('新增权限');
                updateRolePermissionRes = await currentRole.addMenus(currentMenu);
            }
            if (!updateRolePermissionRes) {
                throw ({message: '分配权限失败'})
            }
            ctx.body = {
                status: 0,
                msg: '分配权限成功'
            }
        } catch (e) {
            console.log(e);
            ctx.body = {
                status: 1,
                msg: e.message,
            }
        }
    }

    async getRolePermissionById(ctx) {
        const {id} = ctx.params
        if (!id) {
            return ctx.body = {
                msg: '未上传角色Id',
                status: 1
            }
        }
        try {
            const currentRole = await getRoleById(id, false)
            if (!currentRole) {
                return ctx.body = {
                    status: 1,
                    msg: '未找到当前角色'
                }
            }
            const currentRolePermission = await currentRole.getMenus()
            let permissionIdArr = []
            const menus = await getMenuServe();
            let newMenu = []
            if (currentRolePermission) {
                currentRolePermission.forEach(permission => {
                    permissionIdArr.push(permission.id)
                })
                // const permissionId = permissionIdArr.split(',')
                newMenu = menus.map(menu => {
                    if (permissionIdArr.includes(menu.id)) {
                        menu.select = true;
                    }
                    return menu;
                })
                newMenu = dataToTree(JSON.parse(JSON.stringify(newMenu)), 0)
            } else {
                newMenu = dataToTree(JSON.parse(JSON.stringify(menus)), 0)
            }
            ctx.body = {
                status: 0,
                msg: '获取成功',
                data: newMenu
            }
        } catch (e) {
            ctx.body = {
                status: 1,
                msg: e.message
            }
        }
    }
}

module.exports = new RolePermissionController();
